//实现用户修改功能
const { User } = require('../../model/user');
const bcrypt = require('bcrypt')
module.exports = async(req, res, next) => {
    const id = req.query.id;
    const { username, password, email, role, state } = req.body;
    //获取数据库中的密码
    let user = await User.findOne({ _id: id });
    //密码比对
    const isValid = await bcrypt.compare(password, user.password);
    if (isValid) {
        //密码比对成功
        await User.updateOne({ _id: id }, {
            username: username,
            email: email,
            role: role,
            state: state,
        });
        res.redirect('/admin/user');
    } else {
        //如果密码比对失败，则触发错误处理中间价，将页面重定向回修改页，并且将错误信息传给模板渲染
        let obj = {

            path: '/admin/user-edit',
            message: '密码错误，修改失败',
            id: id
        }
        next(JSON.stringify(obj));
    }


}